// ~ CopyRight © 2012 USTC SINOVATE  SOFTWARE CO.LTD All Rights Reserved.
package com.cmcc.zysoft.infocollection.util;

import org.apache.http.HttpHost;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.conn.params.ConnRoutePNames;
import org.apache.http.impl.client.DefaultHttpClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.cmcc.zysoft.framework.utils.HttpClientTool;


/**
 * @author 闫豆
 * @mail yan.dou@ustcinfo.com
 * @date 2013-4-9 下午4:32:07
 */
public class OpenMasUtil {
	
	private static Logger _logger = LoggerFactory.getLogger(OpenMasUtil.class);
	
	
//	private static String  APPLICATIONID = "DefaultApplicationXSGJ";
//	private static String  PASSWORD = "yCsGnaPEYSXSGJ";
//	
//	private static final String SERVICE_URL = "http://211.138.183.2:9080/openmasservice"; 
	
	
	//http://120.209.131.144:9094/smms/smms/provider/sms?phone=13856992205&msg=hello&spid=f851030f3dfc5283013dfc609c470000
	private static final String SPID = "f851030f3dfc5283013dfc609c470000";
	private static final String URL = "http://120.209.131.144:9094/smms/smms/provider/sms?phone=%s&msg=%s&spid=%s";
	
	/**
	 * @param phone 多个用“，”隔开
	 * @param msg 发送内容
	 */
	public static String sendSms(String phone,String msg){
		_logger.debug("#预警即时短信发送：{}",msg);
		boolean isProxy = false;
		try {
			//String message = new String(msg.getBytes("ISO-8895-1"),"UTF_8");
			String uri = String.format(URL, phone,msg,SPID).replaceAll(" ", "%20");
			HttpClient httpClient=  new DefaultHttpClient();
			if(isProxy){
				HttpHost proxy = new HttpHost("proxy.ah.cmcc",8080, "http");
				httpClient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
			}
			HttpGet httpGet = new HttpGet(uri);
			httpClient.execute(httpGet);
			//httpClient.getConnectionManager().shutdown();//关闭操作
			return "SUCCESS";
		} catch (Exception e) {
			e.printStackTrace();
			_logger.error("预警发送短信失败：{}",e);
			return "FAIL";
		}
	}
	
	
	/**
	 * @param phone 多个用“，”隔开
	 * @param msg 发送内容
	 */
	/*public static void sendSms(String phone,String msg){
		try {
			System.out.println("短信发送========"+msg);
			URL url = new URL(String.format(URL, phone,msg,SPID));
			URLConnection rulConnection = url.openConnection();// 此处的urlConnection对象实际上是根据URL的 
			HttpURLConnection httpUrlConnection = (HttpURLConnection) rulConnection; 
			// 设置是否向httpUrlConnection输出，因为这个是post请求，参数要放在 
			// http正文内，因此需要设为true, 默认情况下是false; 
			httpUrlConnection.setDoOutput(true); 
			// 设置是否从httpUrlConnection读入，默认情况下是true; 
			httpUrlConnection.setDoInput(true); 
			// Post 请求不能使用缓存 
			httpUrlConnection.setUseCaches(false); 
			// 连接，从上述第2条中url.openConnection()至此的配置必须要在connect之前完成， 
	        httpUrlConnection.connect(); 
	        byte[] bot=new byte[1024];
	        //需要读取响应信息
	        InputStream inStream =httpUrlConnection.getInputStream(); 
	        //因为返回的是json的格式，用缓冲流村起来
	        ByteArrayOutputStream ot1=new ByteArrayOutputStream();
	        while(true){
	        	int len=inStream.read(bot);
	        	if(len<=0){
	        		break;
	        	}
	            ot1.write(bot,0,len);
	        }
	        //关闭
	        inStream.close();
	        httpUrlConnection.disconnect();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (Exception e){
			e.printStackTrace();
		}
	}*/
	
	/**
	 * 发送短信.
	 * @param msg 信息内容
	 * @param destinationAddresses 手机号码字符串数组
	 * @return 
	 * 返回类型：String
	 */
	public static void send(String msg,String[] destinationAddresses){
		
	}
	
	public static void main(String[] args){
		try {
			String uri = String.format(URL, "18855174900","dd",SPID);
			System.out.println("uri==="+uri);
			String respone = HttpClientTool.get("proxy.ah.cmcc","8080",uri);
			System.out.println("返回值====="+respone);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
